const User = require('../../models/user');

module.exports = (req, res) => {
  var formData = JSON.parse(req.body.formData)
  console.log(formData);
  var myList = {
    username: formData.myForm.myName,
    user_id: formData.myForm.myId,
    avatar: formData.myForm.myAvatar,
    request_time: formData.myForm.request_time,
    tips_type: '好友请求',
    content:'加个好友，一起交流下~'
  }
  var friendsList = {
    username: formData.friendsForm.friendsName,
    user_id: formData.friendsForm.friendsId,
    avatar: formData.friendsForm.friendsAvatar,
    request_time: formData.friendsForm.request_time,
    tips_type: '好友请求',
    content:'加个好友，一起交流下~'
  }
  // console.log(myList);
  // console.log(friendsList);

  /* 
    添加好友
  */
  if (formData.code === 0) {
    /* 
      请求方添加请求历史记录
    */
    User.findById({ _id: myList.user_id }, (err, userDoc) => {
      /* 
        不能重复添加好友
        1、首先判断好友存不存在
        2、在判断好友请求
      */
      var friends_list = []
      for (let i = 0; i < userDoc.friends_circle.request_list.length; i++) {
        const element = userDoc.friends_circle.request_list[i];
        friends_list.push(element.username)
      }
      /* 
        判断好友存不存在
       */
      if (friends_list.indexOf(friendsList.username) === -1) {
        var userList = []
        for (let i = 0; i < userDoc.friends_circle.request_list.length; i++) {
          const element = userDoc.friends_circle.request_list[i]
          userList.push(element.username)
        }
        // console.log(userList);
        /* 
          开始判断请求是否存在
        */
        if (userList.indexOf(friendsList.username) === -1) {
          /* 
            插入数据
          */
          userDoc.friends_circle.request_list.push(friendsList)
          // console.log(userDoc.friends_circle.request_list);
          userDoc.last_modified_time = new Date
          /*
            更新数据到数据库
          */
          User.findByIdAndUpdate({
            _id: myList.user_id
          }, userDoc, (err) => {
            /* 
              被请求方添加被请求记录
            */
            User.findById({ _id: friendsList.user_id }, (err, friendsUserDoc) => {
              friendsUserDoc.friends_circle.requested_list.push(myList)
              // console.log(friendsUserDoc);
              friendsUserDoc.last_modified_time = new Date
              User.findByIdAndUpdate({ _id: friendsList.user_id }, friendsUserDoc, (err) => {
                console.log('被请求方添加被请求记录成功');
              })
            })
            res.status(200).json({
              err_code: 0,
              message: '好友请求发送成功'
            })
          })
        } else {
          console.log('已发送请求，请勿重复发送')
          res.status(200).json({
            err_code: 1,
            message: '已发送请求，请勿重复发送'
          })
        }
      } else {
        console.log('好友已存在，请勿重复添加')
        res.status(200).json({
          err_code: 2,
          message: '已发送请求，请勿重复发送'
        })
      }
    })
  }
  /* 
    关注作者
  */
  if (formData.code === 1) {
    /* 
      1、关注者添加被关注的用户信息
      2、被关注者添加关注者用户信息
    */
    User.findById({ _id: myList.user_id }, (err, userDoc) => {
      var attentionList = []
      for (let index = 0; index < userDoc.friends_circle.attention_list.length; index++) {
        const element = userDoc.friends_circle.attention_list[index];
        attentionList.push(element.username)
      }
      /* 
        判断是否已经关注
      */
      if (attentionList.indexOf(friendsList.username) === -1) {
        userDoc.friends_circle.attention_list.push({
          username: friendsList.username,
          user_id: friendsList.user_id,
          avatar: friendsList.avatar,
          tips_type: '关注',
          request_time:friendsList.request_time
        })
        userDoc.last_modified_time = new Date
        User.findByIdAndUpdate({ _id: myList.user_id }, userDoc, (err) => {
          /* 
            被关注用户添加记录
          */
          User.findById({ _id: friendsList.user_id }, (err, friendsDoc) => {
            friendsDoc.friends_circle.be_focused_list.push({
              username: myList.username,
              user_id: myList.user_id,
              avatar: myList.avatar,
              tips_type: '关注',
              request_time:myList.request_time
            })
            friendsDoc.last_modified_time = new Date
            User.findByIdAndUpdate({ _id: friendsList.user_id }, friendsDoc, (err) => {
              console.log('被关注用户添加记录成功');
            })
          })
          res.status(200).json({
            err_code: 0,
            message: '关注成功'
          })
        })
      } else {
        console.log('你已关注过该用户，请勿重复关注');
        res.status(200).json({
          err_code: 1,
          message: '你已关注过该用户，请勿重复关注'
        })
      }
    })
  }


}
